<?php
/**
 * Created by PhpStorm.
 * User: 钊鑫
 * Date: 2019/1/26
 * Time: 9:53
 */
namespace app\api\controller;

use app\api\service\Token as TokenService;
use app\api\service\Project as ProjectService;
use app\api\model\Project as ProjectModel;
use app\api\validate\Project as ProjectValidate;
use app\api\validate\Id as IdValidate;

class MyProject extends Base
{
    //获取我的项目(学生)
    public function studentgain(){
        $uid = TokenService::getCurrentUid();
        $result = ProjectModel::where(['parent_id'=>$uid,'publish'=>'1'])->select();
        foreach($result as $value) {
            $data[$value['year']] = $value;
            unset($data[$value['year']]['year']);
        }
        return $this->successMessage($data);
    }

    //获取我的项目（老师）
    public function teachergain(){
        $uid = TokenService::getCurrentUid();
        $uid = '61';
        $result = ProjectModel::whereLike('teacher',"%$uid%")->where(['publish'=>'1'])->select();
        foreach($result as $value) {
            $data[$value['year']][] = $value;
            unset($data[$value['year']]['year']);
        }
        return $this->successMessage($data);
    }

    //编辑项目（学生）
    public function handle(){
       // array('1' => '自然科学类学术论文','2' => '哲学社会科学类社会调查报告和学术论文','3' => '科技发明制作','4' => '创业计划','5' => '创业实践');
        $validate = new ProjectValidate();
        $validate->goCheck();
        $post = $validate->getDataByRule(input('post.'));//过滤非验证器中的字段
        $uid = TokenService::getCurrentUid();
        $result = ProjectService::handle($post,$uid);
        if($result['status'] == 1) {
            return $this->successMessage($result['msg']);
        }else if($result['status'] == -1) {
            $this->errorMessage($result['msg']);
        }else {
            $this->errorMessage('其他未知错误，请重新申请项目！');
        }
        return $result;
    }

    //查看项目（老师）
    public function detail($id = ''){
        $validate = new IdValidate();
        $validate->goCheck();
        $new = new ProjectModel();
        $result = $new->with(['detail'])->where(['id'=>$id,'publish'=>'1'])->find();
        $this->errorMessage($result);
    }
}